package com.prj.ufdm.cloud.ms.auth.api;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.prj.ufdm.cloud.base.dto.RetData;
import com.prj.ufdm.cloud.base.exception.BaseException;
import com.prj.ufdm.cloud.base.util.BaseUuidUtil;
import com.prj.ufdm.cloud.ms.auth.bpo.SysOrganBPO;
import com.prj.ufdm.cloud.ms.auth.model.SysOrgan;
import com.prj.ufdm.cloud.ms.core.bpo.SeqNumBPO;
import com.prj.ufdm.cloud.ms.core.enums.CodeGenerateEnum;
import com.prj.ufdm.cloud.ms.core.model.PageBean;
import com.prj.ufdm.cloud.ms.core.model.SeqNum;

/**
 * 组织机构控制类
 * @author 
 */
@Controller
@RequestMapping("/api/auth/sysorgan")
public class SysOrganController {

	@Autowired
	private SysOrganBPO sysOrganBPO;
	
	@Autowired
	private SeqNumBPO seqNumBPO;

	/**
	 * 分页查询所有机构
	 * @return
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping("/doGetSysOrganList")
    @ResponseBody
    public RetData<PageInfo<SysOrgan>> getOrganList(SysOrgan sysOrgan,PageBean pageBean) throws Exception{
		RetData<PageInfo<SysOrgan>> retData = new RetData<PageInfo<SysOrgan>>();
        sysOrgan.setId(BaseUuidUtil.getUUID());
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<SysOrgan> sysOrganList=sysOrganBPO.doGetList(sysOrgan);
        PageInfo<SysOrgan> pageInfo = new PageInfo<SysOrgan>(sysOrganList);
        retData.setRetBody(pageInfo);
        return retData;
    }
	
	/**
	 * 查询所有机构（JSON格式层次结构）
	 * @return 
	 * @author 胡义振  
	 * @date 2018年3月26日
	 */
	@RequestMapping(value = "/doGetSysOrganObject")
	@ResponseBody
	public RetData<List<JSONObject>> doGetSysOrganObject(SysOrgan sysOrgan) throws Exception {
		RetData<List<JSONObject>> retData = new RetData<List<JSONObject>>();
		List<SysOrgan> listSysOrgan = sysOrganBPO.doGetList(sysOrgan);
		retData.setRetBody(sysOrganBPO.doExchangeSysOrganListToJson(listSysOrgan));
		return retData;
	}

	/**
	 * DESC: 新增机构
	 * @auther 胡义振
	 * @date 2020年1月16日
	 * @param sysOrgan
	 * @return
	 * @throws Exception 
	 * @return
	 */
    @RequestMapping("/doSaveSysOrgan")
    @ResponseBody
    public RetData<String> doSaveSysOrgan(SysOrgan sysOrgan) throws Exception{
        RetData<String> retData=new RetData<String>();
        sysOrgan.setId(BaseUuidUtil.getUUID());
        //生成编码
        SeqNum seqNum = new SeqNum();
		seqNum.setCodeType(CodeGenerateEnum.ORGAN_CODE_GENERATE.getMessage());
        String organCode=seqNumBPO.generateCode(seqNum);
        if(StringUtils.isBlank(organCode)) {
        	throw new BaseException("机构编码生成失败，联系管理员！");
        }
        sysOrgan.setOrganCode(organCode);
        sysOrganBPO.doSave(sysOrgan);
        return retData;
    }
	

    /**
     * DESC: 修改机构
     * @auther 胡义振
     * @date 2020年1月16日
     * @param sysOrgan
     * @return
     * @throws Exception 
     * @return
     */
    @RequestMapping(value = "/doModSysOrgan")
    @ResponseBody
    public RetData<String> doModSysOrgan(SysOrgan sysOrgan) throws Exception {
        RetData<String> retData = new RetData<String>();
        sysOrganBPO.doModById(sysOrgan);
        return retData;
    }
    
	/**
	 * 删除机构，递归
	 * @param sysOrgan
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/doDelSysOrgan")
	@ResponseBody
	public RetData<String> deleteChildrenOrganListByOrganId(SysOrgan sysOrgan) throws Exception {
		RetData<String> retData = new RetData<String>();
		sysOrganBPO.deleteChildrenOrganListByOrganId(sysOrgan);
		return retData;
	}
	
}
